﻿(function () {
    $(function () {
        var _var;
        var _$dataTable = $('#DataTable');
        var _dataService = abp.services.app.bill; //对应application中接口前缀
        //对应AppPermissions里面的Pages_BasicDataModule_Classrooms_Create等变量
        var _permissions = {
            create: abp.auth.hasPermission('Pages.FinanceModule.Bills.Create'),
            edit: abp.auth.hasPermission('Pages.FinanceModule.Bills.Edit'),
            'delete': abp.auth.hasPermission('Pages.FinanceModule.Bills.Delete')
        };
        //新增/编辑收入窗体定义
        var _createOrEditIncomeModal = new app.ModalManager({
            viewUrl: abp.appPath + 'FinanceModule/Bills/CreateOrEditIncomeModal',
            scriptUrl: abp.appPath + 'Areas/FinanceModule/Views/Bills/_CreateOrEditIncomeModal.js',
            modalClass: 'CreateOrEditIncomeModal'//类名跟_CreateOrEditModal.js文件里面的类名app.modals.CreateOrEditModal一致，就能调用模态窗口了
        });
        //新增编辑支出窗体定义
        var _createOrEditExpendModal = new app.ModalManager({
            viewUrl: abp.appPath + 'FinanceModule/Bills/CreateOrEditExpendModal',
            scriptUrl: abp.appPath + 'Areas/FinanceModule/Views/Bills/_CreateOrEditExpendModal.js',
            modalClass: 'CreateOrEditExpendModal'//类名跟_CreateOrEditModal.js文件里面的类名app.modals.CreateOrEditModal一致，就能调用模态窗口了
        });
        //表格定义，包括样式，触发事件等。
        _$dataTable.jtable({

            //title: app.localize('Pages_BasicDataModule_Classrooms'),
            paging: true,
            sorting: true,
            multiSorting: true,

            actions: {
                listAction: {
                    method: _dataService.getObjects
                }
            },

            fields: {
                id: {
                    key: true,
                    list: false
                },
                actions: {
                    title: app.localize('Actions'),
                    width: '10%',
                    sorting: false,
                    list: _permissions.edit || _permissions.delete,
                    display: function (data) {
                        var $span = $('<span></span>');

                        if (_permissions.edit) {
                            $('<button class="btn btn-default btn-xs" title="' + app.localize('Edit') + '"><i class="fa fa-edit"></i></button>')
                                .appendTo($span)
                                .click(function () {
                                    if(data.record.billTypeId==1)
                                    {
                                        _createOrEditIncomeModal.open({ id: data.record.id });
                                    }
                                    else if(data.record.billTypeId==2)
                                    {
                                        _createOrEditExpendModal.open({ id: data.record.id });
                                    }
                                });
                        }

                        if (_permissions.delete) {
                            $('<button class="btn btn-default btn-xs" title="' + app.localize('Delete') + '"><i class="fa fa-trash-o"></i></button>')
                                .appendTo($span)
                                .click(function () {
                                    deleteObject(data.record);
                                });
                        }

                        return $span;
                    }
                },
                //网格里面字段必须驼峰写法
                billDate: {
                    title: app.localize('Pages_FinanceModule_Bills_GridColumn_BillDate'),
                    width: '10%',
                    display: function (data) {
                        return moment(data.record.billDate).format('L');
                    }
                },
                schoolName: {
                    title: app.localize('Pages_FinanceModule_Bills_GridColumn_SchoolName'),
                    width: '20%'
                },
                billAccount:{
                    title: app.localize('Pages_FinanceModule_Bills_GridColumn_BillAccount'),
                    width: '20%'
                },
                billTypeId: {
                    title: app.localize('Pages_FinanceModule_Bills_GridColumn_BillType'),
                    width: '10%',
                    display: function (data) {
                        if (data.record.billTypeId == 1) {
                            return app.localize('Pages_FinanceModule_Bills_GridColumn_Income');
                        } else if(data.record.billTypeId==2){
                            return '<span style="color: rgb(253, 140, 6);">' + app.localize('Pages_FinanceModule_Bills_GridColumn_Expend') + '</span>';
                        }
                    }
                },
                incomeExpendType: {
                    title: app.localize('Pages_FinanceModule_Bills_GridColumn_IncomeExpendType'),
                    width: '10%'
                },

                incomeAmount: {
                    title: app.localize('Pages_FinanceModule_Bills_GridColumn_IncomeAmount'),
                    width: '10%',
                    display: function (data) {
                        if (data.record.incomeAmount == 0) {
                            return '';
                        }
                        else
                        {
                            return data.record.incomeAmount;
                        }
                    }
                },
                expendAmount: {
                    title: app.localize('Pages_FinanceModule_Bills_GridColumn_ExpendAmount'),
                    width: '10%',
                    display: function (data) {
                        if (data.record.expendAmount == 0) {
                            return '';
                        }
                        else
                            return data.record.expendAmount;
                    }
                }
            }

        });//表格定义结束

        //加载数据方法
        function getObjects(reload) {
            if (reload) {
                _$dataTable.jtable('reload');
            } else {
                _$dataTable.jtable('load', {
                    filter: $('#DataTableFilter').val()
                });
            }
        }
        //删除账单信息
        function deleteObject(o) {
            abp.message.confirm(
                app.localize('Com_Msg_Delete_Warning', o.billDate+o.schoolName +o.billAccount),
                function (isConfirmed) {
                    if (isConfirmed) {
                        _dataService.deleteObject({
                            id: o.id
                        }).done(function () {
                            getObjects(true);
                            abp.notify.success(app.localize('SuccessfullyDeleted'));
                        });
                    }
                }
            );
        }
        //多条件查询框按钮
        $('#GetListButton').click(function (e) {
            e.preventDefault();
            getObjects();
        });
        //打开新增/编辑收入页面
        $('#CreateNewButton').click(function (e) {
            e.preventDefault();
            _createOrEditIncomeModal.open();
        });
        //支出页面
        $('#CreateExpendButton').click(function (e) {
            e.preventDefault();
            _createOrEditExpendModal.open();
        });
        //新增/编辑保存之后回到主页面，重新加载主页面数据。
        abp.event.on('app.CreateOrEditModalSaved', function (result) {
            //alert('return value:' + result.classroomName);
            getObjects(true);
        });

        //加载数据
        getObjects();

        //光标定位到查询条件输入框
        $('#DataTableFilter').focus();
    });
})();